package com.zx.ecode.ui.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zx.ecode.DataExampleUtil;
import com.zx.ecode.R;
import com.zx.ecode.controller.adapter.EpidemicListAdapter;
import com.zx.ecode.model.entity.EpidemicBean;
import com.zzp.basepresenter.ui.fragment.BaseFragment;
import com.zzp.basepresenter.util.LayoutUtil;

import org.json.JSONException;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EpidemicFragment extends BaseFragment {
    private static EpidemicFragment instance;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;

    public static EpidemicFragment getInstance() {
        if (instance == null) {
            synchronized (EpidemicFragment.class) {
                if (instance == null) {
                    instance = new EpidemicFragment();
                }
            }
        }
        return instance;
    }

    @Override
    public void bindButter(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_epidemic;
    }

    @Override
    protected void lazyLoad(Activity context, View view) {
        tvTitle.setLayoutParams(LayoutUtil.getInstance().getRelativeLayoutMarginParams(context, tvTitle));
        try {
            setRecycleView(DataExampleUtil.getEpidemic());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void setRecycleView(List<EpidemicBean> list) {
        //去除刷新条目闪动动画
        ((DefaultItemAnimator) Objects.requireNonNull(recycleView.getItemAnimator())).setSupportsChangeAnimations(false);
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        EpidemicListAdapter adapter = new EpidemicListAdapter(context, list);
        recycleView.setAdapter(adapter);
    }
}
